/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pig.demo.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.pig.common.security.service.PigUser;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.pig.demo.entity.XlmOrder;
import com.pig4cloud.pig.demo.entity.XlmSetting;
import com.pig4cloud.pig.demo.entity.XlmTask;
import com.pig4cloud.pig.demo.service.XlmOrderService;
import com.pig4cloud.pig.demo.service.XlmSettingService;
import com.pig4cloud.pig.demo.service.XlmTaskService;
import com.pig4cloud.pig.demo.util.OrderUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 任务
 *
 * @author xiaoxiong
 * @date 2022-04-08 18:01:37
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/rw")
@Api(value = "rw", tags = "任务管理")
public class XlmTaskController {

    private final XlmTaskService xlmTaskService;

    private final XlmOrderService xlmOrderService;

    private final XlmSettingService xlmSettingService;


    /**
     * 确认下单
     *
     * @param id
     */
    @PostMapping("/confirm")
    @ApiOperation(value = "确定抢单", notes = "确定抢单")
    public R confirm(Integer shopid, Integer taskid) {
        XlmSetting setbyid = xlmSettingService.getById(4);
        PigUser user = SecurityUtils.getUser();
        if (xlmTaskService.getusertoday(String.valueOf(user.getId()), taskid) > Integer.valueOf(setbyid.getValue())) {
            return R.failed("今日抢单数量超过上限");
        }
        XlmOrder o = new XlmOrder();
        OrderUtils orderUtils = new OrderUtils();
        o.setOrderid(orderUtils.getOrderCode((int) ((Math.random() * 9 + 1) * 100000)));
        o.setTaskid(taskid);
        o.setPayid(String.valueOf(user.getId()));
        o.setShopid(shopid);
        o.setStatus(0);
        o.setReturnmoney(xlmTaskService.getById(taskid).getReturnmoney());
        o.setMoney(xlmTaskService.getById(taskid).getMoney());
        boolean save = xlmOrderService.save(o);
        if (save == true) {
            return R.ok("抢单成功");
        }
        return R.failed("抢单失败");
    }

    /**
     * 分页查询
     *
     * @param page    分页对象
     * @param xlmTask 任务
     * @return
     */
    @Inner(value = false)
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getXlmTaskPage(Page page, String longitude, String latitude, XlmTask xlmTask) {
        return R.ok(xlmTaskService.getuserTaskpage(page, longitude, latitude, xlmTask));
    }

    /**
     * 分页查询
     *
     * @param page    分页对象
     * @param xlmTask 任务
     * @return
     */
    @Inner(value = false)
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/pages")
//    @PreAuthorize("@pms.hasPermission('demo_xlmtask_get')")
    public R getXlmTaskPages(Page page, XlmTask xlmtask) {

        return R.ok(xlmTaskService.page(page, Wrappers.query(xlmtask)));
    }

    /**
     * 通过id查询任务
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('demo_xlmtask_get')")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(xlmTaskService.getById(id));
    }

    /**
     * 新增任务
     *
     * @param xlmTask 任务
     * @return R
     */
    @ApiOperation(value = "新增任务", notes = "新增任务")
    @SysLog("新增任务")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('demo_xlmtask_add')")
    public R save(@RequestBody XlmTask xlmTask) {
        return R.ok(xlmTaskService.saveOrUpdate(xlmTask));
    }

    /**
     * 修改任务
     *
     * @param xlmTask 任务
     * @return R
     */
    @ApiOperation(value = "修改任务", notes = "修改任务")
    @SysLog("修改任务")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('demo_xlmtask_edit')")
    public R updateById(@RequestBody XlmTask xlmTask) {
        return R.ok(xlmTaskService.updateById(xlmTask));
    }

    /**
     * 通过id删除任务
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除任务", notes = "通过id删除任务")
    @SysLog("通过id删除任务")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('demo_xlmtask_del')")
    public R removeById(@PathVariable Integer id) {
        return R.ok(xlmTaskService.removeById(id));
    }

}
